import store from "@/store";
export function checkAuth(value) {
	const { rules = [] } = store?.getters || {};
	if (rules == "*") return true;

	if (!value || !value.length) {
		console.error(`need rule! Like v-auth="'auth'" or v-auth="['auth']"`);
		return false;
	}
	if (typeof value === "string") {
		return rules.includes(value);
	}
	if (Array.isArray(value)) {
		return rules.some(rule => value.includes(rule));
	}
	return false;
}
export default {
	inserted(el, binding) {
		const { value } = binding;
		let hasAuth = checkAuth(value);
		// el.style.display = hasAuth ? "" : "none";
		if (!hasAuth) {
			el.parentNode && el.parentNode.removeChild(el);
		}
	},
	update(el, binding) {
		const { value } = binding;
		let hasAuth = checkAuth(value);
		if (!hasAuth) {
			el.parentNode && el.parentNode.removeChild(el);
		}
	}
};
